package cn.com.pajx.pajx_smart_platform.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import butterknife.ButterKnife;
import cn.com.pajx.pajx_smart_platform.mvp.view.IBaseView;
import cn.com.pajx.pajx_smart_platform.tools.AppManager;
import cn.com.pajx.pajx_smart_platform.tools.ToastUtil;
import cn.com.pajx.pajx_smart_platform.ui.view.LoadingDialog;
import okhttp3.ResponseBody;

/**
 * Created by ruancw on 2023/04/03.
 * activity的基类
 */
public abstract class BaseActivity extends AppCompatActivity implements IBaseView {
    protected Context mContext;
    protected LoadingDialog mLoadingDialog;//进度对话框

    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
        super.onCreate(savedInstanceState);
        mContext = this;
        beforeSetContentView();
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        dynamicHideNavigationBar();
        mLoadingDialog = new LoadingDialog(mContext);
        handleSavedInstanceState(savedInstanceState);
        initView();
        //将Activity实例添加到AppManager的堆栈
        AppManager.getInstance().addActivity(this);

    }

    protected void beforeSetContentView() {
    }

    protected abstract int getLayoutId();

    protected abstract void initView();

    protected void handleSavedInstanceState(Bundle savedInstanceState) {

    }

    /**
     * 是否显示标题栏返回键
     *
     * @return 默认显示 true
     */
    protected boolean isShowBackView() {
        return true;
    }

    /**
     * 是否显示删除按钮
     *
     * @return 默认不显示 false
     */
    protected boolean isShowDeleteView() {
        return false;
    }

    /**
     * 是否显示标题栏
     *
     * @return 默认显示 true
     */
    protected boolean hasHeadTitle() {
        return true;
    }

    /**
     * 隐藏底部导航栏
     */
    public void dynamicHideNavigationBar() {
        Window window;
        window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE | View.SYSTEM_UI_FLAG_FULLSCREEN;
        window.setAttributes(params);

        int uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                | View.SYSTEM_UI_FLAG_FULLSCREEN; // hide status bar

        uiFlags |= 0x00001000;
        getWindow().getDecorView().setSystemUiVisibility(uiFlags);
        //保持屏幕常亮
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    @Override
    public void showProgress() {
        if (mLoadingDialog != null) {
            mLoadingDialog.show();
        }
    }

    @Override
    public void showProgress(String message) {
        if (mLoadingDialog != null) {
            mLoadingDialog.setMessage(message).show();
        }
    }

    @Override
    public void hideProgress() {
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
    }

    @Override
    public void onSuccess(String data, String message, int code, String tag) {

    }

    @Override
    public void onDownloadSuccess(ResponseBody responseBody) {

    }

    @Override
    public void onHtmlSuccess(ResponseBody body) {

    }

    @Override
    public void onError(String message, int code, String tag) {
        if (code == 300) {
            exitToLogin();
        } else {
            ToastUtil.toast(message);
        }
    }

    @Override
    public void onFailed(Throwable throwable) {

    }

    @Override
    public void onBackPressed() {
        if (getSupportFragmentManager().getBackStackEntryCount() <= 0) {
            onClickBack();
        } else {
            getSupportFragmentManager().popBackStack();
        }
    }

    //监听返回键
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (getSupportFragmentManager().getBackStackEntryCount() <= 0) {
                onClickBack();
            } else {
                getSupportFragmentManager().popBackStack();
            }
            return false;
        } else if (keyCode == KeyEvent.KEYCODE_MENU) {
            onRefresh();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }


    protected void onClickBack() {
        finish();
        super.onBackPressed();
    }

    protected void onRefresh() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //将Activity实例从AppManager的堆栈中移除
        AppManager.getInstance().removeActivity(this);
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
            mLoadingDialog = null;
        }
    }

    /**
     * 退出程序
     * <p>
     * 令牌过期，
     */
    public void exitToLogin() {
        startActivity(new Intent(mContext, SplashActivity.class));
        finish();
    }

    /**
     * 关闭软键盘
     */
    protected void hideSoftInput() {
        // 虚拟键盘隐藏 判断view是否为空
        View decorView = getWindow().peekDecorView();
        if (decorView != null) {
            // 隐藏虚拟键盘
            InputMethodManager inputManger = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
            assert inputManger != null;
            inputManger.hideSoftInputFromWindow(decorView.getWindowToken(), 0);
        }
    }

    // 判断当前EditText是否可滚动
    protected boolean canVerticalScroll(EditText editText) {
        //滚动的距离
        int scrollY = editText.getScrollY();
        //控件内容的总高度
        int scrollRange = editText.getLayout().getHeight();
        //控件实际显示的高度
        int scrollExtent = editText.getHeight() - editText.getCompoundPaddingTop() - editText.getCompoundPaddingBottom();
        //控件内容总高度与实际显示高度的差值
        int scrollDifference = scrollRange - scrollExtent;
        if (scrollDifference == 0) {
            return false;
        }
        return (scrollY > 0) || (scrollY < scrollDifference - 1);
    }
}
